<template>
  <div class="custom-number-input">
    <button class="decrease-btn" @click="decrease" :disabled="value <= min">-</button>
    <input
      type="text"
      :value="value"
      @input="handleInput"
      @blur="handleBlur"
      class="number-input"
    />
    <button class="increase-btn" @click="increase" :disabled="value >= max">+</button>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Prop, Watch } from 'vue-property-decorator'

@Component({
  name: 'NumberInput'
})
export default class extends Vue {
  @Prop({ default: 1 }) readonly value!: number
  @Prop({ default: 1 }) readonly min!: number
  @Prop({ default: 99 }) readonly max!: number

  handleInput(event: Event) {
    const target = event.target as HTMLInputElement
    let val = parseInt(target.value) || 0
    
    if (val < this.min) val = this.min
    if (val > this.max) val = this.max
    
    this.$emit('input', val)
  }

  handleBlur(event: Event) {
    const target = event.target as HTMLInputElement
    let val = parseInt(target.value) || this.min
    
    if (val < this.min) val = this.min
    if (val > this.max) val = this.max
    
    this.$emit('input', val)
  }

  decrease() {
    if (this.value > this.min) {
      this.$emit('input', this.value - 1)
    }
  }

  increase() {
    if (this.value < this.max) {
      this.$emit('input', this.value + 1)
    }
  }
}
</script>

<style lang="scss" scoped>
.custom-number-input {
  display: flex;
  align-items: center;
  width: 120px;
  height: 40px;
  border: 1px solid #333333;
  border-radius: 4px;
  overflow: hidden;

  .decrease-btn,
  .increase-btn {
    width: 32px;
    height: 100%;
    border: none;
    background: #fff;
    color: #333333;
    font-size: 20px;
    cursor: pointer;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;

    &:hover {
      background-color: #f5f5f5;
    }

    &:disabled {
      cursor: not-allowed;
      color: #c0c4cc;
    }
  }

  .decrease-btn {
    border-right: 1px solid #333333;
  }

  .increase-btn {
    border-left: 1px solid #333333;
  }

  .number-input {
    flex: 1;
    height: 100%;
    border: none;
    text-align: center;
    font-size: 14px;
    color: #333333;
    padding: 0;
    margin: 0;
    width: 54px;

    &:focus {
      outline: none;
    }
  }
}
</style> 